package com.brkj.util.view;

import java.util.ArrayList;
import java.util.List;

import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;

import com.brkj.main.MainTabActivity;
import com.brkj.main.R;
import com.brkj.util.view.BaseActionBarItem.itemClick;

public class BaseActionBarActivity extends BaseCoreActivity {


	private PopupWindow popupWindow;
	private PopupWindow rightBtnPopupWindow;
	private BaseActionBarSelectAdapter adapter;
	private BaseActionBarSelectAdapter rightBtnAdapter;
	private View titleView;
	protected ImageView rightBtnView;
	private TextView title;
	private View titleArraw;

	private List<BaseActionBarItem> itemList = new ArrayList<BaseActionBarItem>();
	private List<BaseActionBarItem> rightBtnItemList = new ArrayList<BaseActionBarItem>();

    public void setSlideMenu(){
        View slideMenuView = findViewById(R.id.slideMenuView);
        slideMenuView.setVisibility(View.VISIBLE);
        slideMenuView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                MainTabActivity.sm.toggle();
            }
        });
    }

    public void setReturnBtn(){
        ImageView leftBtn = (ImageView) findViewById(R.id.btn_left);

        leftBtn.setVisibility(View.VISIBLE);

        leftBtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }


    private void initRightBtnView(){
        rightBtnView =(ImageView)findViewById(R.id.btn_right);

        if(rightBtnView!= null){
            rightBtnView.setVisibility(View.VISIBLE);
            rightBtnView.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    if(rightBtnPopupWindow.isShowing()){
                        rightBtnPopupWindow.dismiss();
                    }else{
//                        rightBtnPopupWindow.showAsDropDown(rightBtnView, 0, 0);
                        rightBtnPopupWindow.showAtLocation(rightBtnView, Gravity.NO_GRAVITY, 0, 0);
                    }
                }
            });
        }

        initRightBtnPopUpWindow();
    }
	
	private void initTitleView(){
		titleView = findViewById(R.id.titleView);
		title = (TextView) findViewById(R.id.title);
        titleArraw =  findViewById(R.id.titleArraw);
        titleArraw.setVisibility(View.VISIBLE);

		if(titleView!= null){
			titleView.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View arg0) {
					if(popupWindow.isShowing()){
						popupWindow.dismiss();
					}else{
                        WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
                        int xPos = -popupWindow.getWidth() / 2
                                + windowManager.getDefaultDisplay().getWidth() / 2;

                        popupWindow.showAtLocation(titleView, Gravity.NO_GRAVITY, 0, 0);
//                        popupWindow.showAsDropDown(titleView, 0, 0);
//                        popupWindow.showAtLocation(titleView, Gravity.CENTER_HORIZONTAL, xPos, titleView.getBottom());
					}
				}
			});
		}
		
		initPopUpWindow();
	}
	public void addTitleButtonItem(String itemName, itemClick clickListener){
		//还没初始化titleButton
		if(titleView==null){
			initTitleView();
		}
		
		BaseActionBarItem item = new BaseActionBarItem();
		item.setItemName(itemName);
		item.setClickListener(clickListener);
		
		itemList.add(item);
		
		if(adapter!= null){
			adapter.notifyDataSetChanged();
		}
		
		if(title!=null){
			title.setText(itemList.get(0).getItemName());
		}
	}


    public void addRightButtonItem(String itemName, itemClick clickListener){
        //还没初始化titleButton
        if(rightBtnView==null){
            initRightBtnView();
        }

        BaseActionBarItem item = new BaseActionBarItem();
        item.setItemName(itemName);
        item.setClickListener(clickListener);

        rightBtnItemList.add(item);

        if(rightBtnAdapter!= null){
            rightBtnAdapter.notifyDataSetChanged();
        }

    }

    public void setActivityTitle(String titleText){
        if(title== null){
            title = (TextView) findViewById(R.id.title);
        }

        title.setText(titleText);
    }

	private void initPopUpWindow(){
		
		View popupWindow_view = getLayoutInflater().inflate(
				R.layout.popupwindow_title_button_click, null, false);
		popupWindow = new PopupWindow(popupWindow_view,
				LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, false);

		popupWindow.setFocusable(true);
		popupWindow.setOutsideTouchable(true);
		// popupWindow.setAnimationStyle(R.style.PopWindowAnim);
//		popupWindow.setBackgroundDrawable(new ColorDrawable(0x7DC0C0C0));
		
		GridView gridview = (GridView) popupWindow_view.findViewById(R.id.gridview);
		View bgView = (View) popupWindow_view.findViewById(R.id.bgView);
        bgView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });

		adapter = new BaseActionBarSelectAdapter(this, itemList, popupWindow, title);
		
		gridview.setAdapter(adapter);
	}

    private void initRightBtnPopUpWindow(){

		View popupWindow_view = getLayoutInflater().inflate(
				R.layout.popupwindow_right_button_click, null, false);
        rightBtnPopupWindow = new PopupWindow(popupWindow_view,
				LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, false);

        rightBtnPopupWindow.setFocusable(true);
        rightBtnPopupWindow.setOutsideTouchable(true);
		// popupWindow.setAnimationStyle(R.style.PopWindowAnim);
//        rightBtnPopupWindow.setBackgroundDrawable(new ColorDrawable(0x7DC0C0C0));

        View bgView = (View) popupWindow_view.findViewById(R.id.bgView);
        bgView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                rightBtnPopupWindow.dismiss();
            }
        });

        GridView gridview = (GridView) popupWindow_view.findViewById(R.id.gridview);

        rightBtnAdapter = new BaseActionBarSelectAdapter(this, rightBtnItemList, rightBtnPopupWindow, null);

		gridview.setAdapter(rightBtnAdapter);
	}

    public void setRightBtnBackground(int resID){
        if(rightBtnView != null){
            rightBtnView.setImageResource(resID);
        }
    }

    public void setRightBtnBackgroundAndListener(int resID, OnClickListener listener){

        rightBtnView =(ImageView)findViewById(R.id.btn_right);

        rightBtnView.setVisibility(View.VISIBLE);

        if(rightBtnView != null){
            rightBtnView.setImageResource(resID);
        }

        rightBtnView.setOnClickListener(listener);
    }
}
